/*
 * @Author: liuYang codkly@gmail.com
 * @Date: 2022-10-26 22:46:47
 * @LastEditors: lycodk lycodk@gmail.com
 * @LastEditTime: 2022-11-10 14:08:55
 * @FilePath: \my_project\blog_v1.1\blog_web\utils\http.ts
 * @Description: 
 * 暂无备注
 */
import { hash } from 'ohash'

const fetch = (url: string, options?: any) => {
    // console.log('log000=>', { ...options })

    options['headers'] = { url: url }
    // options['server'] = server
    return new Promise((resolve, reject) => {
        // if (process.server) {
        // console.log('options111', options)
        const key = hash(JSON.stringify(options))
        console.log('==options', options, 'key', key)
        useFetch('/api' + url, { ...options, key }).then(({ data, error }) => {
            if (error.value) {
                console.log('error==>', error.value)
                reject(error.value)
                return
            }
            const value: any = data.value
            console.log('value:===>', value)
            if (!value) {
                // 这里处理错误回调
                reject(value)
            } else {
                resolve(value)

            }
        }).catch((err: any) => {
            console.log('err', err)
            reject(err)
        })
        // }else{
        //     console.log('process', process)
        // }

    })
}

export default new class Http {

    get(url: string, params?: any, server?: Boolean) {
        console.log('=>url', url)
        return fetch(url, { method: 'get', params, server: server })
    }

    post(url: string, body?: any) {
        return fetch(url, { method: 'post', body })
    }

    put(url: string, body?: any) {
        return fetch(url, { method: 'put', body })
    }

    delete(url: string, body?: any) {
        return fetch(url, { method: 'delete', body })
    }
}